/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.sh.ndt.exam.endpoint;

import com.sh.ndt.exam.entity.ExamHistory;
import com.sh.ndt.exam.entity.TestHistory;
import com.sh.ndt.exam.entity.TestHistoryParam;
import com.sh.ndt.exam.service.ExamService;
import com.sh.ndt.exception.ServiceException;
import com.sh.ndt.question.service.QuestionService;
import com.sh.ndt.schedule.entity.ExamScheduleParam;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UserEntitlement;
import com.sh.ndt.user.service.UserEntitlementService;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Prashanth
 */
@Controller
@RequestMapping("/examService")
public class ExamEndpoint {
    private static final Logger LOGGER = Logger.getLogger(ExamEndpoint.class);
    
    public ExamEndpoint() {
        LOGGER.debug("ExamEndpoint is initialized ********************** ");
    }
    
    @Autowired
    QuestionService questionService;
    
    @Autowired
    ExamService examService;
    
    @Autowired
    UserEntitlementService userEntitlementService;
    
    @RequestMapping(value = "/markAsFailed",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )@ResponseBody
    public UserEntitlement markAsFailed(@RequestBody ExamScheduleParam examScheduleParam) {
        UserDashboardDetail userDashBoardDetail = new UserDashboardDetail();
        try {
            
            String message = questionService.markAsFailed(examScheduleParam.getScheduleId(),examScheduleParam.getScheduleStatus());
            
            UserEntitlement userEntitilement = new UserEntitlement();
            
            // Set User
            // Set User's Entitlement (Dashboard Details)
            
            userDashBoardDetail = userEntitlementService.getUserDashboardDetail((int)examScheduleParam.getUserId());
            userDashBoardDetail.setErrorMessage(message);
            
            userEntitilement.setUserDashboardDetail(userDashBoardDetail);
            
            return userEntitilement;            
            
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return null;
    }    

    @RequestMapping(value = "/markAsPassed",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )@ResponseBody
    public UserEntitlement markAsPassed(@RequestBody ExamScheduleParam examScheduleParam) {
        UserDashboardDetail userDashBoardDetail = new UserDashboardDetail();
        try {
            
            String message = questionService.markAsPassed(examScheduleParam.getScheduleId(),examScheduleParam.getScheduleStatus());
            
            UserEntitlement userEntitilement = new UserEntitlement();
            
            // Set User
            // Set User's Entitlement (Dashboard Details)
            
            userDashBoardDetail = userEntitlementService.getUserDashboardDetail((int)examScheduleParam.getUserId());
            userDashBoardDetail.setErrorMessage(message);
            
            userEntitilement.setUserDashboardDetail(userDashBoardDetail);
            
            return userEntitilement;            
            
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return null;
    }    
    
    @RequestMapping(value = "/updateMockSession",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )@ResponseBody
    public boolean updateMockSession(@RequestBody TestHistoryParam testHistoryParam) {
        try {
            
            String message = questionService.updateMockSession(testHistoryParam.getUserId(),testHistoryParam.getTestId(),
                    testHistoryParam.getResult(),testHistoryParam.getCreditPoint());
            LOGGER.debug("Test History Update Status: " + message);
            
            return true;
            
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return false;
    }    
    
    
    @RequestMapping(value = "/getTestHistory",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )@ResponseBody
    public List<TestHistory> getTestHistory(@RequestBody TestHistoryParam testHistoryParam) {
        try {
            return examService.getTestHistoryList(testHistoryParam.getUserId(), testHistoryParam.getTestId());
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return null;
    }    

    @RequestMapping(value = "/getExamHistory",
            method = RequestMethod.POST,
            consumes = "application/xml",
            produces = "application/json"
    )@ResponseBody
    public List<ExamHistory> getExamHistory(@RequestBody TestHistoryParam examHistoryParam) {
        try {
            return examService.getExamHistoryList(examHistoryParam.getUserId(), examHistoryParam.getTestId());
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        return null;
    } 

}
